package com.qxy.practice.concurrent;




import java.util.concurrent.*;

/**
 * @author : 戚欣扬
 * @code : 三个售票员售票demo
 */

class Ticket2{
    private int nums = 50;

    public synchronized  void saleTicket(){
        if (nums > 0) {
            System.out.println(Thread.currentThread().getName() + " 卖出了第 " + (nums--) + " 张票, 剩余: " + nums);
        }
    }
}
public class SaleTicket2 {


    public static void main(String[] args) {
        Ticket2 ticket2 = new Ticket2();
        ExecutorService pool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors()-14,
                Runtime.getRuntime().availableProcessors()-14,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        try {
            for (int i = 0; i < 50; i++) {
                pool.execute(() -> {
                    ticket2.saleTicket();
                });
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            pool.shutdown();
        }


    }
}


